package com.example.vlille2.proximite;

import java.util.List;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.example.vlille2.R;
import com.example.vlille2.accueil.GeneralActivity;
import com.example.vlille2.accueil.StationsAdapter;
import com.example.vlille2.base.Station;
import com.example.vlille2.base.StationsBDD;
import com.example.vlille2.recherche.RechercheActivity;

public class ProximiteActivity extends GeneralActivity {

	private TextView tv_distance;
	private SeekBar barre;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.layout_proximite);
		
		//gestion seekBar
		barre = (SeekBar)findViewById(R.id.seekBar_distance);
		barre.setMax(RechercheActivity.MAX_DISTANCE);
		barre.setProgress(RechercheActivity.DISTANCE_DEFAUT);
		
		tv_distance = (TextView)findViewById(R.id.tv_distance);
		tv_distance.setText(""+barre.getProgress());
		
		barre.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				((ProximiteAdapter)adapter).setRayon(arg0.getProgress());
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				tv_distance.setText(arg1+"");
			}
		});
		
		if (lastUserLocation == null) activerControleDistance(false);
		
		ListView lv = (ListView)findViewById(R.id.lv_listeProximite);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnClicListeListener());
	}	

	@Override
	protected StationsAdapter getAdapter() {
		StationsBDD bdd = StationsBDD.getInstance();
		
		bdd.open();		
		List<Station> liste = bdd.getStations();
		bdd.close();
		
		return new ProximiteAdapter(this, liste, lastUserLocation);
	}
	
	@Override
	protected void locationChanged() {
		super.locationChanged();
		activerControleDistance(true);
	}
	
	private void activerControleDistance(boolean activer)
	{
		int couleur;
		if(activer) couleur = getResources().getColor(R.color.noir);
		else couleur = getResources().getColor(R.color.disabled);
		
		TextView tv = (TextView)findViewById(R.id.tv_preDistance);
		tv.setTextColor(couleur);
		
		tv_distance.setTextColor(couleur);
		
		tv = (TextView)findViewById(R.id.tv_apres_distance);
		tv.setTextColor(couleur);
		
		barre.setEnabled(false);
	}
}
